package com.woniuxy.redisson;

import com.woniuxy.config.RedisConfig;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
@Service
public class RedissonResult {
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private RedissonClient redissonClient;

    public Integer reduce() throws Exception {
        RLock lock = redissonClient.getLock("key");
        boolean b = lock.tryLock(20, 20, TimeUnit.SECONDS);
        if(b){
            Integer goods = (Integer) redisConfig.opsForValue().get("goods");
            goods = goods-1;
            redisConfig.opsForValue().set("goods",goods);
        }
        return -1;
    }

}
